<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
       一般来说，子串子数组这种都可以用暴力法，找出所有的子数组，然后判断。
       优化方法一般来说都是滑动窗口
      */
      var lengthOfLongestSubstring = function (s) {
        let resLength = 0
        let map = new Map()
        let left = 0
        for (let i = 0; i < s.length; i++) {
          if (!map.has(s[i])) {
            map.set(s[i], i)
            //这是为了避免没有重复字符的
            resLength = Math.max(resLength, i - left + 1)
          } else {
            //这里有个细节在于 map.get(s[i])必须大于之前的left，不然会导致回退
            if (map.get(s[i]) >= left) {
              left = map.get(s[i]) + 1
              //这个只是为了确保边界不会回退
            }
            resLength = Math.max(resLength, i - left + 1)
            map.set(s[i], i)
          }
        }
        return resLength
      }
      lengthOfLongestSubstring('abcadcbb')
    </script>
  </body>
</html>
